package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.PrinterTemplateDAO;
import com.junzhiit.nengji.shop.model.PrinterTemplate;

@Repository
public class PrinterTemplateDAOImpl extends AbstractBaseDAO<PrinterTemplate> implements PrinterTemplateDAO {
	
	@Override
	public List<PrinterTemplate> findPageAll(PrinterTemplate printerTemplate,int pageNumber, int maxPageRow){
		
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from printer_template");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(printerTemplate.getShopId())){
			sql.append(" and shop_id=?");
			param.setString(printerTemplate.getShopId());
		}
		
		sql.append(" order by created_time desc");
		
		return super.queryForPagingList(sql.toString(),PrinterTemplate.class,param,pageNumber, maxPageRow);
	}
	
	@Override
	public int getTotalCount(PrinterTemplate printerTemplate){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from printer_template");
		sql.append(" where is_delete=?");
		
		
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(printerTemplate.getShopId())){
			sql.append(" and shop_id=?");
			param.setString(printerTemplate.getShopId());
		}
		
		return super.queryForInt(sql.toString(),param);
	}
	
	@Override
	public List<PrinterTemplate> getAll(PrinterTemplate printerTemplate){
		
		StringBuffer sql=new StringBuffer();
		sql.append(" select id,name from printer_template");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(printerTemplate.getShopId())){
			sql.append(" and shop_id=?");
			param.setString(printerTemplate.getShopId());
		}
		
		return super.queryForList(sql.toString(), PrinterTemplate.class,param);
	}
	
	@Override
	public int getUseCount(String id,PrinterTemplate printerTemplate){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from printer");
		sql.append(" where is_delete=?");
		sql.append(" and printer_template_id=?");
		sql.append(" and shop_id=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(id);
		param.setString(printerTemplate.getShopId());
		
		return super.queryForInt(sql.toString(),param);
	}
	
	
	
	
}
